package com.lengyu.vxminiprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lengyu.vxminiprogram.common.HttpResultDto;
import com.lengyu.vxminiprogram.entity.Car;
import com.lengyu.vxminiprogram.entity.TakePeople;
import com.lengyu.vxminiprogram.mapper.TakePeopleMapper;
import com.lengyu.vxminiprogram.service.ICarService;
import com.lengyu.vxminiprogram.service.ITakePeopleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyu
 * @since 2024-11-21
 */
@Service
public class TakePeopleServiceImpl extends ServiceImpl<TakePeopleMapper, TakePeople> implements ITakePeopleService {

    @Autowired
    ICarService carService;

    @Override
    public String addPeople(TakePeople people) {
        saveOrUpdate(people);
        Car car = carService.getById(people.getCarId());
        if(car != null){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("car_id",car.getId());
            List<TakePeople> takePeopleList = list(queryWrapper);
            List<String> peopleIdList = new ArrayList<>();
            if(takePeopleList != null && takePeopleList.size() > 0){
                takePeopleList.forEach(i->{
                    peopleIdList.add(i.getUserId());
                });
            }
            String result = String.join(",", peopleIdList);

            car.setTakePeopleIds(result);
            carService.updateById(car);

        }
       return people.getId();
    }
}
